//
//  PurchaseNowTableViewCell1.m
//  ChunLaiZhuangClient
//
//  Created by xielihui on 16/4/12.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "PurchaseNowTableViewCell1.h"
#import "ProdcutDetaiModel.h"

@interface PurchaseNowTableViewCell1()
@property (weak, nonatomic) IBOutlet UIImageView *imageIV;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *productNumberLabel;
@property(nonatomic, assign) NSInteger productNumber;
@property(nonatomic, strong) ProdcutDetaiModel *model;
@property (weak, nonatomic) IBOutlet UIView *productCountContainer;
@property (weak, nonatomic) IBOutlet UIButton *addProductNumBtn;
@property (weak, nonatomic) IBOutlet UIButton *minusProductNumBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *spaceLineHeightCons;

@end

@implementation PurchaseNowTableViewCell1

- (void)awakeFromNib {
    self.imageIV.layer.borderColor = [UIColor colorWithRGB:0xe1e1e1].CGColor;
    self.imageIV.layer.borderWidth = 1.f;
    self.productCountContainer.layer.borderWidth = 1.f;
    self.productCountContainer.layer.cornerRadius = 15;
    self.productCountContainer.layer.borderColor = [UIColor colorWithRGB:0xe1e1e1].CGColor;
    self.spaceLineHeightCons.constant = .33f;
    // Initialization code
}

- (void)updateWithProductInfo:(ProdcutDetaiModel *)model {
    self.model = model;
    [self.imageIV sd_setImageWithURL:[NSURL URLWithString:model.data.images[0]]];
    self.nameLabel.text = [NSString stringWithFormat:@"%@", self.model.data.price];
    self.productNumberLabel.text =  [NSString stringWithFormat:@"%@", model.data.minimum];
    self.productNumber = model.data.minimum.intValue;
    [self setBtnState];
}

- (void)setBtnState {
    if (self.productNumber > self.model.data.minimum.integerValue) {
        self.minusProductNumBtn.enabled = YES;
    } else {
        self.minusProductNumBtn.enabled = NO;
    }
}

- (void)updatePriceLabel:(NSString *)price {
    self.nameLabel.text = [NSString stringWithFormat:@"%@", price];
}

- (IBAction)minusProductNumberAction:(id)sender {
    if(self.model.data.minimum.intValue + 1 == self.productNumber) {
        self.minusProductNumBtn.enabled = NO;
    } else {
        self.minusProductNumBtn.enabled = YES;
    }
    self.productNumberLabel.text = [NSString stringWithFormat:@"%ld", --self.productNumber];
    if (self.minusProductNumBlcok) {
        self.minusProductNumBlcok(self.productNumber);
    }
}

- (IBAction)addProductNumberAction:(id)sender {
    self.productNumberLabel.text = [NSString stringWithFormat:@"%ld", ++self.productNumber];
    [self setBtnState];
    if (self.addProductNumBlock) {
        self.addProductNumBlock(self.productNumber);
    }
}

- (IBAction)closePageAction:(id)sender {
    if (self.closePageBlock) {
        self.closePageBlock();
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
